﻿!function () {
	if (window.Zving) {
		return;
	}
	var z = window.Zving = {
		version : 2.1
	};
	var getCurrentScript = function (base) {
		if (document.currentScript) {
			return document.currentScript.src; //FF,Chrome
		};
		var stack;
		try {
			a.b.c(); //强制报错,以便捕获e.stack
		} catch (e) { //safari的错误对象只有line,sourceId,sourceURL
			stack = e.stack;
			if (!stack && window.opera) {
				//opera 9没有e.stack,但有e.Backtrace,但不能直接取得,需要对e对象转字符串进行抽取
				stack = (String(e).match(/of linked script \S+/g) || []).join(" ");
			}
		}
		if (stack) {
			stack = stack.split(/[@ ]/g).pop(); //取得最后一行,最后一个空格或@之后的部分
			stack = stack[0] === "(" ? stack.slice(1, -1) : stack.replace(/\s/, ""); //去掉换行符
			return stack.replace(/(:\d+)?:\d+$/i, ""); //去掉行号与或许存在的出错字符起始位置
		}
		var nodes = (base ? document : head).getElementsByTagName("script"); //只在head标签中寻找
		for (var i = nodes.length, node; node = nodes[--i]; ) {
			if (node.readyState === "interactive") {
				return node.src;
			}
		}
		var node=nodes[nodes.length - 1];
		return node.hasAttribute ? node.src : node.getAttribute("src", 4); 
	}

	var jspath = getCurrentScript(true);
	var scripts = document.getElementsByTagName('script'),
	script = document.currentScript || scripts[scripts.length - 1];

	z.JSLIBPATH = jspath.substr(0, jspath.lastIndexOf('/') + 1);
	z.CONTEXTPATH = script.getAttribute('contextpath');
	z.Config= { namespace: 'window' };


	if (z.JSLIBPATH.indexOf(location.protocol + '//' + location.host + '/') == 0) {
		z.JSLIBPATH = z.JSLIBPATH.replace(location.protocol + '//' + location.host, '');
	}
	//再外部没配置应用路径时才使用默认路径
	if(!z.CONTEXTPATH){
		z.CONTEXTPATH = z.JSLIBPATH.replace(/[^\/]+\/?$/, '');
		if(z.CONTEXTPATH.indexOf('/preview/') != -1){
			z.CONTEXTPATH=z.CONTEXTPATH.substr(0,z.CONTEXTPATH.indexOf('preview/'));
		}
	}
	/**
	加载脚本
	url:js文件路径，因有加z.PATH，所以路径是相对于js框架根目录开始
	 **/
	z.importJS = z.importJs = function (url) {
		if (!/^\/|^\w+\:\/\//.test(url)) {
			url = z.JSLIBPATH + url;
		}
		if (!document.body || document.readyState == 'loading') {
			document.write('<script type="text/javascript" src="' + url + '"><\/script>');
		} else {
			z.loadJS(url);
		}
	};

	/**
	加载CSS文件
	url:css文件路径，因有加z.PATH，所以路径是相对于js框架根目录开始
	 **/
	z.importCSS = z.importCss = function (url, win) {
		win = win && z.isWindow(win) ? win : window;
		var document = win.document;

		if (!/^\/|^\w+\:\/\//.test(url)) {
			url = z.JSLIBPATH + url;
		}
		if (!document.body || document.readyState == 'loading') {
			document.write('<link rel="stylesheet" type="text/css" href="' + url + '" />');
		} else {
			z.loadCSS(url);
		}

	};
	/**
	异步加载脚本
	url:js文件路径，相对于引用js框架的页面，如果要从js框架根目录开始引用需自行加上z.JSLIBPATH
	onsuccess:js文件加载后的回调函数
	 **/
	z.loadJS = z.loadJs = function (url, onsuccess) {
		var head = document.getElementsByTagName('head')[0] || document.documentElement,
		script = document.createElement('script'),
		done = false;
		script.src = url;
		script.onerror = script.onload = script.onreadystatechange = function () {
			if (!done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) {
				done = true;
				if (onsuccess) {
					onsuccess();
				}
				script.onerror = script.onload = script.onreadystatechange = null;
				//head.removeChild(script);
			}
		};
		head.appendChild(script);
	};

	/**
	异步加载CSS文件
	url:css文件路径，相对于引用js框架的页面，如果要从js框架根目录开始引用需自行加上z.JSLIBPATH
	 **/
	//往指定的同源页面窗口加载样式文件（求url为相对于win中页面的地址）
	z.loadCSS = z.loadCss = function (url, win) {
		win = win && z.isWindow(win) ? win : window;
		var document = win.document;

		var head = document.getElementsByTagName('head')[0] || document.documentElement;
		if (document.createStyleSheet) { //注意：IE11的不再支持document.createStyleSheet
			document.createStyleSheet(url);
		} else {
			var e = document.createElement('link');
			e.rel = 'stylesheet';
			e.type = 'text/css';
			e.href = url;
			head.appendChild(e);
		}
	};

	if (!/:\/\/(develop)/.test(location.href) || !Object.defineProperty) {
		z.importCSS('default.css');
		z.importCSS('style/default.css');
		z.importCSS('components.css');
		z.importJS('vendors.min.js');
		z.importJS('components.min.js');
	}

}
();